/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.xuexiang.xuidemo.slice.imageview.preview.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.item.ImageViewInfo;
import com.xuexiang.xuidemo.item.NineGridInfo;
import com.xuexiang.xuidemo.provider.XulNineGridListProvider;
import com.xuexiang.xuidemo.refresh.IRefresh;
import com.xuexiang.xuidemo.refresh.SwipeRefreshLayout;
import com.xuexiang.xuidemo.refresh.TextOverComponent;

import java.util.ArrayList;
import java.util.List;

import static com.xuexiang.xui_lib.util.DisplayUtils.getDisplayHeightInPx;
import static com.xuexiang.xuidemo.util.DisplayUtils.getDisplayWidthInPx;

/**
 * 九宫格图片
 *
 * @since 2021-04-08
 */
public class NineGridImageViewSlice extends AbilitySlice {
    private Text mSwitch;
    private Image mImageBack;
    private List<NineGridInfo> nineGridInfos = new ArrayList<>();
    private  List<ImageViewInfo> itemList = new ArrayList<>();
    private List<ImageViewInfo> itemList1 = new ArrayList<>();
    private List<ImageViewInfo> itemList2 = new ArrayList<>();
    private List<ImageViewInfo> itemList3 = new ArrayList<>();
    private XulNineGridListProvider xulNineGridListProvider;
    private SwipeRefreshLayout refresh;
    private HmEventHandler mEventHandler;
    private EventRunner runner;
    private Runnable task;
    private final int delayTime = 250;
    private int displayHeight,displayWidth;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_grid_image);
        initView();
        initData();
        initHandler();
        initRefresh();
        refresh.refresh();
    }

    private void initView() {
        mImageBack = (Image) findComponentById(ResourceTable.Id_image_back);
        mSwitch = (Text) findComponentById(ResourceTable.Id_tx_switch);
        displayHeight =  getDisplayHeightInPx(getContext());
        displayWidth = getDisplayWidthInPx(getContext());
      /*  if (displayWidth > displayHeight) {
        }*/
        mSwitch.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminateAbility();
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName(getBundleName())
                    .withAbilityName("com.xuexiang.xuidemo.slice.imageview.preview.GridVideoAbility")
                    .build();
                intent.setOperation(operation);
                startAbility(intent);
                setTransitionAnimation(0,0);
            }
        });
        mImageBack.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminateAbility();
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")
                    .withBundleName(getBundleName())
                    .withAbilityName("com.xuexiang.xuidemo.slice.imageview.preview.PreviewImageAbility")
                    .build();
                intent.setOperation(operation);
                startAbility(intent);
                setTransitionAnimation(0,0);
            }
        });

    }
    private void initRefresh() {
        refresh = (SwipeRefreshLayout) findComponentById(ResourceTable.Id_refresh);

        // 创建下拉刷新头部组件
        TextOverComponent overView = new TextOverComponent(getContext());

        // 设置下拉刷新的头部组件
        refresh.setRefreshOverComponent(overView);
        refresh.setRefreshListener(new IRefresh.RefreshListener() {
            @Override
            public void onRefresh() {
                mEventHandler.postTask(task, delayTime, EventHandler.Priority.IMMEDIATE); // 延时250ms后立即处理
            }
            /**
             * 允许下拉刷新
             *
             * @return  boolean
             */
            @Override
            public boolean enableRefresh() {
                return true;
            }
        });
    }
    private void initHandler() {
        runner = EventRunner.getMainEventRunner();
        task = new Runnable() {
            @Override
            public void run() {
                xulNineGridListProvider.notifyDataChanged();
                refresh.refreshFinish();
            }
        };
        mEventHandler = new HmEventHandler(runner);
    }
    private void initData() {
        itemList.add(new ImageViewInfo("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=141294069" 
            + ",864810829&fm=26&gp=0.jpg"));
        itemList.add(new ImageViewInfo("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2860640912" 
            + ",1833965317&fm=26&gp=0.jpg"));
        NineGridInfo nineGridInfo = new NineGridInfo();
        nineGridInfo.setContent("我是一只喵，快乐的星猫～～～");
        nineGridInfo.setImgUrlList(itemList);
        nineGridInfos.add(nineGridInfo);
        itemList1.add(new ImageViewInfo("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=141294069" 
            + ",864810829&fm=26&gp=0.jpg"));
        itemList1.add(new ImageViewInfo("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2860640912" 
            + ",1833965317&fm=26&gp=0.jpg"));
        itemList1.add(new ImageViewInfo("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3933345026" 
            + ",3409802114&fm=26&gp=0.jpg"));
        itemList1.add(new ImageViewInfo("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1107220453" 
            + ",145887914&fm=26&gp=0.jpg"));
        itemList1.add(new ImageViewInfo("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1107220453" 
            + ",145887914&fm=26&gp=0.jpg"));
        itemList1.add(new ImageViewInfo("https://photocdn.sohu.com/20160307/mp62252655_1457334772519_2.png"));
        itemList1.add(new ImageViewInfo("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3933345026" 
            + ",3409802114&fm=26&gp=0.jpg"));
        NineGridInfo nineGridInfo1 = new NineGridInfo();
        nineGridInfo1.setContent("我是一只喵，快乐的星猫～～～");
        nineGridInfo1.setImgUrlList(itemList1);
        nineGridInfos.add(nineGridInfo1);
        itemList2.add(new ImageViewInfo("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=141294069" 
            + ",864810829&fm=26&gp=0.jpg"));
        itemList2.add(new ImageViewInfo("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2860640912" 
            + ",1833965317&fm=26&gp=0.jpg"));
        itemList2.add(new ImageViewInfo("https://photocdn.sohu.com/20160307/mp62252655_1457334772519_2.png"));
        itemList2.add(new ImageViewInfo("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3933345026" 
            + ",3409802114&fm=26&gp=0.jpg"));
        NineGridInfo nineGridInfo2 = new NineGridInfo();
        nineGridInfo2.setContent("我是一只喵，快乐的星猫～～～");
        nineGridInfo2.setImgUrlList(itemList2);
        nineGridInfos.add(nineGridInfo2);
        itemList3.add(new ImageViewInfo("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=141294069" 
            + ",864810829&fm=26&gp=0.jpg"));
        itemList3.add(new ImageViewInfo("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2860640912" 
            + ",1833965317&fm=26&gp=0.jpg"));
        itemList3.add(new ImageViewInfo("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3933345026" 
            + ",3409802114&fm=26&gp=0.jpg"));
        itemList3.add(new ImageViewInfo("https://photocdn.sohu.com/20160307/mp62252655_1457334772519_2.png"));
        itemList3.add(new ImageViewInfo("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1107220453" 
            + ",145887914&fm=26&gp=0.jpg"));
        itemList3.add(new ImageViewInfo("https://photocdn.sohu.com/20160307/mp62252655_1457334772519_2.png"));
        itemList3.add(new ImageViewInfo("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2860640912" 
            + ",1833965317&fm=26&gp=0.jpg"));
        itemList3.add(new ImageViewInfo("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2860640912" 
            + ",1833965317&fm=26&gp=0.jpg"));
        itemList3.add(new ImageViewInfo("https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3933345026" 
            + ",3409802114&fm=26&gp=0.jpg"));
        NineGridInfo nineGridInfo3 = new NineGridInfo();
        nineGridInfo3.setContent("我是一只喵，快乐的星猫～～～");
        nineGridInfo3.setImgUrlList(itemList3);
        nineGridInfos.add(nineGridInfo3);
        ListContainer mGridImage = (ListContainer) findComponentById(ResourceTable.Id_lc_grid_image);
        xulNineGridListProvider = new XulNineGridListProvider(nineGridInfos, this);
        mGridImage.setItemProvider(xulNineGridListProvider);
    }
    /**
     * HmEventHandler
     *
     * @since 2021-04-08
     */
    public static class HmEventHandler extends EventHandler {
        private HmEventHandler(EventRunner runner) throws IllegalArgumentException {
            super(runner);
        }

        @Override
        protected void processEvent(InnerEvent event) {
            super.processEvent(event);
            int id = event.eventId;
            long param = event.param;
        }
    }
}
